package me.aip.mov.minio;

public enum FileExtensionEnum {

    BMP("bmp", "424D", 5 * 1024 * 1024),
    JPG("jpg", "FFD8FF", 5 * 1024 * 1024),
    JPEG("jpeg", "FFD8FF", 5 * 1024 * 1024),
    PNG("png", "89504E470D0A1A0A", 5 * 1024 * 1024),
    GIF("gif", "GIF8", 5 * 1024 * 1024),
    //==========================
    AVI("avi", "41564920", 50 * 1024 * 1024),
    WMV("wmv", "3026b2758e66cf11", 50 * 1024 * 1024),
    MPG("mpg", "000001B", 50 * 1024 * 1024),
    MPEG("mpeg", "000001B", 50 * 1024 * 1024),
    QUICKTIME("mov", "6D6F6F76", 50 * 1024 * 1024),
    REAL_MEDIA("rm", "2E524D46", 50 * 1024 * 1024),
    REAL_AUDIO("ram", "2E7261FD", 50 * 1024 * 1024),
    SWF("swf", "435753/465753/5a5753", 50 * 1024 * 1024),
    FLV("flv", "464c56", 50 * 1024 * 1024),
    PDF("pdf", "255044462D312E", 50 * 1024 * 1024),
    MP4("mp4", "0000001466747970/0000001866747970/0000001c66747970", 50 * 1024 * 1024),

    //==========================================
    DEFAULT("default", "", 20 * 1024 * 1024);
    private String fileExtension;

    private String fileHead;

    private int limitSize;

    public static FileExtensionEnum queryByFileExtension(String fileName) {
        String fileExtension = fileName.substring(fileName.lastIndexOf(".")+1);
        for (FileExtensionEnum value : FileExtensionEnum.values()) {
            if (value.fileExtension.equals(fileExtension)) {
                return value;
            }
        }
        return DEFAULT;
    }

    /**
     * 校验文件类型和文件头是否匹配并判断大小
     * @param fileName
     * @param fileHead
     * @param length
     * @return
     */
    public  static  Boolean checkLength(String fileName, byte [] fileHead,long length){
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        FileExtensionEnum fileExtensionEnum = queryByFileExtension(fileExtension);
//        if (!checkFileHeader(fileExtension,fileHead)) {
//            throw new RuntimeException("文件类型和真实类型不符，请检查！");
//        }
       return fileExtensionEnum.limitSize >= length;
    }

    /**
     * 校验文件尾缀和文件头代表的类型是否相符，防止手改文件
     * @param fileName
     * @param fileHead
     * @return
     */
    public static boolean checkFileHeader(String fileName, byte [] fileHead){
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        FileExtensionEnum fileExtensionEnum = queryByFileExtension(fileExtension);
        if (FileExtensionEnum.DEFAULT.fileHead.equals(fileExtensionEnum.fileHead)){
            return true;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (byte aByte : fileHead) {
            int i = aByte & 0xFF;

            String s = Integer.toHexString(i);
            if (s.length()<2) {

                stringBuffer.append("0");
            }
            stringBuffer.append(s);
        }
        String head = fileExtensionEnum.getFileHead();
        String[] split = head.split("/");
        for (String spl : split) {
            if (stringBuffer.toString().startsWith(spl)) {
                return true;
            }
        }

        return false;

    }


    FileExtensionEnum(String fileExtension, String fileHead, int limitSize) {
        this.fileExtension = fileExtension;
        this.fileHead = fileHead;
        this.limitSize = limitSize;
    }

    public String getFileExtension() {
        return fileExtension;
    }

    public void setFileExtension(String fileExtension) {
        this.fileExtension = fileExtension;
    }

    public String getFileHead() {
        return fileHead;
    }

    public void setFileHead(String fileHead) {
        this.fileHead = fileHead;
    }

    public int getLimitSize() {
        return limitSize;
    }

    public void setLimitSize(int limitSize) {
        this.limitSize = limitSize;
    }
}
